package com.buaa.airticketbookingsystem.Controller;

import com.buaa.airticketbookingsystem.Entity.Airport;
import com.buaa.airticketbookingsystem.Service.AirportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class AddNewAirportController {

    @Autowired
    private AirportService airportService;

    @PostMapping("/admin/addNewAirport")
    public Map<String, Object> addNewAirport(@RequestBody Map<String, String> remap){
        Map<String, Object> map = new HashMap<>();
        String a_id = remap.get("a_id");
        String name = remap.get("name");
        String location = remap.get("location");
        try {
            if (airportService.getAirportById(a_id) == null) {
                airportService.addNewAirport(new Airport(a_id, name, location));
                map.put("res", true);
                map.put("message", "添加机场成功！");
            }
            else {
                map.put("res", false);
                map.put("message", "该机场已经存在！");
            }

        }
        catch (Exception e) {
            map.put("res", false);
            map.put("message", "添加机场失败！");
        }
        return map;
    }

}
